﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace iCollect.Constants
{
    class GlobalConstants
    {
        public const string DEFAULT_PORT = "COM2";
        public const int DEFAULT_MB_INTERVAL = 10;
        public const int DEFAULT_MB_SLAVE = 1;
        public const string DEFAULT_MB_DATA_TYPE = "Decimal";
        public const string QI_LUN_JI = "0";
        public const string DIAN_JI_BENG = "1";


        public static bool isAdmin = false;

        public static string currentDevice;
        public static string type;
        public static string typeLabel;
        public static string assetCode;
        public static string owner;
        public static DateTime sDate;

        public static string user;
        public static string departement;

        public static byte slaveID = DEFAULT_MB_SLAVE;
        public static string port = DEFAULT_PORT;
        public static int mbInterval = 10;
        public static string mbDataType = "Decimal";
        public static Hashtable channelMap = new Hashtable();
        public static Hashtable channelTypeMap = new Hashtable();
        public static Dictionary<String, String[]> channelContentMap = new Dictionary<String, String[]>();
        public static Dictionary<String, List<String>> typeChannelMap = new Dictionary<String, List<String>>();
        public static Dictionary<String, List<ArrayList>> currentData = new Dictionary<String, List<ArrayList>>();
        public static Dictionary<String, List<ArrayList>> exceptionData = new Dictionary<String, List<ArrayList>>();
        //<"PIT3699",[value1, value2]>
        public static Dictionary<String, ArrayList> channelAlert = new Dictionary<String, ArrayList>();


        public static bool configSimOn = true;
        public static bool configExOn = true;
        public static bool configTE = true;
        public static bool configVT = true;
        public static bool configPT = true;
        public static bool configFT = true;
        public static bool configRT = true;
        public static int configTimeLength = 5 * 60;
        public static List<DateTime> configTimePoint = new List<DateTime>();
        public static int configExLength = 50;
        public static int configExMaxCount = 10;
        public static int configExInterval = 10;

        //5 min as default
        public static decimal configFreq = 1;
        public static int displayCount = 60;
        //60 x 20 / saveFreq
        public static int exceptionCount = 1200;

        private static iCollect.Database.RepositoryDataSetTableAdapters.deviceparamTableAdapter deviceparamTableAdapter
            = new iCollect.Database.RepositoryDataSetTableAdapters.deviceparamTableAdapter();

        private static iCollect.Database.RepositoryDataSetTableAdapters.deviceniparamTableAdapter deviceniparamTableAdapter
            = new iCollect.Database.RepositoryDataSetTableAdapters.deviceniparamTableAdapter();

        public static void initCurrentData()
        {
            if (type == "0")
            {
                for (int i = 0; i < modbusChannel1.GetLength(0); i++)
                {
                    currentData[modbusChannel1[i, 1]] = new List<ArrayList>();
                    exceptionData[modbusChannel1[i, 1]] = new List<ArrayList>();
                }
                for (int i = 0; i < NIChannel1.GetLength(0); i++)
                {
                    currentData[NIChannel1[i, 1]] = new List<ArrayList>();
                    exceptionData[NIChannel1[i, 1]] = new List<ArrayList>();
                }
            }

            else if (type == "1")
            {
                for (int i = 0; i < NIChannel2.GetLength(0); i++)
                {
                    currentData[NIChannel2[i, 1]] = new List<ArrayList>();
                    exceptionData[NIChannel2[i, 1]] = new List<ArrayList>();
                }
            }
        }

        private static void loadMBChannelFromDB()
        {
            DataTable mbDataTable = deviceparamTableAdapter.GetDataBy3(currentDevice);
            foreach (DataRow row in mbDataTable.Rows)
            {
                channelMap[row["channel"].ToString()] = row["address"].ToString();
            }
        }

        private static void loadNIChannelFromDB()
        {
            DataTable niDataTable = deviceniparamTableAdapter.GetDataBy3(currentDevice);
            foreach (DataRow row in niDataTable.Rows) 
            {
                channelMap[row["channel"].ToString()] = row["nichannel"].ToString();
            }
        }

        public static void initChannelMap()
        {
            String[,] currentChannel = new String[10, 3];

            if (type == GlobalConstants.QI_LUN_JI)
            {
                int arrayLength = modbusChannel1.GetLength(0) + NIChannel1.GetLength(0);
                currentChannel = new string[arrayLength,5];
                for (int i = 0; i < modbusChannel1.GetLength(0); i++)
                {
                    for (int j = 0; j < modbusChannel1.GetLength(1); j++)
                    {
                        currentChannel[i, j] = modbusChannel1[i, j];
                    }
                }
                for (int i = 0; i < NIChannel1.GetLength(0); i++)
                {
                    int cIndex = i + NIChannel1.GetLength(0) -1;
                    for (int j = 0; j < NIChannel1.GetLength(1); j++)
                    {
                        currentChannel[cIndex, j] = NIChannel1[i, j];
                    }
                }
            }
            else if (type == GlobalConstants.DIAN_JI_BENG)
            {
                currentChannel = (String[,])NIChannel2.Clone();
               
            }

            for (int i = 0; i < currentChannel.GetLength(0); i++)
            {
                channelMap[currentChannel[i,1]] = currentChannel[i,0];
            }

            for (int i = 0; i < currentChannel.GetLength(0); i++)
            {
                channelTypeMap[currentChannel[i, 1]] = currentChannel[i, 2];
            }

            for (int i = 0; i < currentChannel.GetLength(0); i++)
            {
                String[] cc = new String[3];
                cc[0] = currentChannel[i, 2];
                cc[1] = currentChannel[i, 3];
                cc[2] = currentChannel[i, 4];
                channelContentMap[currentChannel[i, 1]] = cc;
            }

            for (int i = 0; i < currentChannel.GetLength(0); i++)
            {
                string channelType = currentChannel[i, 2];
                if(!typeChannelMap.ContainsKey(channelType))
                {
                    typeChannelMap[channelType] = new List<String>();
                }
                typeChannelMap[channelType].Add(currentChannel[i, 1]);
            }

            if (type == GlobalConstants.QI_LUN_JI)
            {
                loadMBChannelFromDB();
            }
            loadNIChannelFromDB();
        }

        private static String[,] modbusChannel1 = {
                                                      {"30042","PIT3699","压力", "汽轮机", "汽轮机入口介质流量"},
                                                      {"30065","PT3697","压力", "汽轮机", "汽轮机背压蒸汽压力1号"},
                                                      {"30068","PIT3649","压力", "压缩机", "压缩机一段入口压力"},
                                                      {"30072","PIT3652","压力", "压缩机", "压缩机二段出口压力"},
                                                      {"30044","FIT3699","流量", "汽轮机", "汽轮机入口流量"},
                                                      {"30048","FT3653","流量", "压缩机", "压缩机二段出口流量"},
                                                      {"30068","FT3649","流量", "压缩机", "压缩机一段入口流量"},
                                                      {"30076","TE3692","温度", "汽轮机", "汽轮机驱动端轴承温度"},
                                                      {"30077","TE3629A","温度", "压缩机", "压缩机驱动端轴承温度"},
                                                      {"30078","TE3630A","温度", "压缩机", "压缩机非驱动端轴承温度"},
                                                      {"30075","TE3694","温度", "汽轮机", "汽轮机非驱动端轴承温度"}
                                                  };

        private static String[,] NIChannel1 = {
                                                      {"PXI1Slot2/ai0","VT3696X","振动", "汽轮机", "汽轮机驱动端轴振动（水平）"},
                                                      {"PXI1Slot2/ai1","VT3697Y","振动", "汽轮机", "汽轮机驱动端轴振动（垂直）"},
                                                      {"PXI1Slot2/ai2","VT3698X","振动", "汽轮机", "汽轮机非驱动端轴振动（水平）"},
                                                      {"PXI1Slot2/ai3","VT3699Y","振动", "汽轮机", "汽轮机非驱动端轴振动（垂直）"},
                                                      {"PXI1Slot2/ai4","VT3629X","振动", "压缩机", "压缩机驱动端轴振动（水平）"},
                                                      {"PXI1Slot2/ai5","VT3629Y","振动", "压缩机", "压缩机驱动端轴振动（垂直）"},
                                                      {"PXI1Slot2/ai6","VT3630X","振动", "压缩机", "压缩机驱动非端轴振动（水平）"},
                                                      {"PXI1Slot2/ai7","VT3630Y","振动", "压缩机", "压缩机非驱动端轴振动（垂直）"},

                                                      {"PXI1Slot3/ai1","ZE3696","轴位移", "汽轮机", "汽轮机轴位移1号"},
                                                      {"PXI1Slot3/ai2","ZE3697","轴位移", "汽轮机", "汽轮机轴位移2号"},
                                                      {"PXI1Slot3/ai3","ZE3629A","轴位移", "压缩机", "压缩机轴位移1号"},
                                                      {"PXI1Slot3/ai4","ZE3629B","轴位移", "压缩机", "压缩机轴位移2号"}
                                                  };

        private static String[,] NIChannel2 = {
                                                      //{"PXI1Slot4/ai0","TE1","温度", "电机", "电机自由端温度"},
                                                      //{"PXI1Slot4/ai1","TE2","温度", "电机", "电机自由端温度"},
                                                      //{"PXI1Slot4/ai2","TE3","温度", "轴承", "轴承座温度（小头）"},
                                                      //{"PXI1Slot4/ai3","TE4","温度", "轴承", "轴承座温度（大头）"},
                                                      //{"PXI1Slot4/ai4","TE5","温度", "泵", "泵壳体温度"},

                                                      //{"PXI1Slot1/ai5","PT1","压力", "泵", "泵出口压力"},

                                                      {"PXI1Slot2/ai0","VT1X","振动", "电机", "泵非驱动端轴承振动（X方向）"},
                                                      {"PXI1Slot2/ai1","VTIY","振动", "电机", "泵非驱动端轴承振动（Y方向）"},
                                                      {"PXI1Slot2/ai2","VT2X","振动", "轴承", "泵驱动端轴承振动（X方向）"},
                                                      {"PXI1Slot2/ai3","VT2Y","振动", "轴承", "泵驱动端轴承振动（Y方向）"},
                                                      {"PXI1Slot2/ai4","VT3Y","振动", "轴承", "电机联轴端机壳振动（Y方向）"},
                                                      {"PXI1Slot2/ai5","VT4Y","振动", "泵", "电机自由端机壳振动（Y方向）"}
                                                  };
    }
}
